package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.PUserAddress;
import com.ruoyi.system.domain.bo.PUserAddressBo;
import com.ruoyi.system.domain.vo.PUserAddressVo;
import com.ruoyi.system.mapper.PUserAddressMapper;
import com.ruoyi.system.service.IPUserAddressService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 收货人地址Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-05
 */
@RequiredArgsConstructor
@Service
public class PUserAddressServiceImpl implements IPUserAddressService {

    private final PUserAddressMapper baseMapper;

    /**
     * 查询收货人地址
     */
    @Override
    public PUserAddressVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询收货人地址列表
     */
    @Override
    public TableDataInfo<PUserAddressVo> queryPageList(PUserAddressBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PUserAddress> lqw = buildQueryWrapper(bo);
        Page<PUserAddressVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询收货人地址列表
     */
    @Override
    public List<PUserAddressVo> queryList(PUserAddressBo bo) {
        LambdaQueryWrapper<PUserAddress> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PUserAddress> buildQueryWrapper(PUserAddressBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PUserAddress> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUid() != null, PUserAddress::getUid, bo.getUid());
        lqw.like(StringUtils.isNotBlank(bo.getRealName()), PUserAddress::getRealName, bo.getRealName());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), PUserAddress::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getProvince()), PUserAddress::getProvince, bo.getProvince());
        lqw.eq(StringUtils.isNotBlank(bo.getCity()), PUserAddress::getCity, bo.getCity());
        lqw.eq(StringUtils.isNotBlank(bo.getDistrict()), PUserAddress::getDistrict, bo.getDistrict());
        lqw.eq(StringUtils.isNotBlank(bo.getDetail()), PUserAddress::getDetail, bo.getDetail());
        lqw.eq(bo.getPostCode() != null, PUserAddress::getPostCode, bo.getPostCode());
        lqw.eq(StringUtils.isNotBlank(bo.getLongitude()), PUserAddress::getLongitude, bo.getLongitude());
        lqw.eq(StringUtils.isNotBlank(bo.getLatitude()), PUserAddress::getLatitude, bo.getLatitude());
        lqw.eq(bo.getIsDefault() != null, PUserAddress::getIsDefault, bo.getIsDefault());
        return lqw;
    }

    /**
     * 新增收货人地址
     */
    @Override
    public Boolean insertByBo(PUserAddressBo bo) {
        if (!ObjectUtils.isEmpty(bo.getId())) {
            return this.updateByBo(bo);
        }
        PUserAddress add = BeanUtil.toBean(bo, PUserAddress.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            if ("1".equals(add.getIsDefault())) {
                this.baseMapper.update(null, new LambdaUpdateWrapper<PUserAddress>().set(PUserAddress::getIsDefault, "0").eq(PUserAddress::getUid, LoginHelper.getUserId()));
                this.baseMapper.update(null, new LambdaUpdateWrapper<PUserAddress>().set(PUserAddress::getIsDefault, "1").eq(PUserAddress::getId, add.getId()));
            }
        }
        return flag;
    }

    /**
     * 修改收货人地址
     */
    @Override
    public Boolean updateByBo(PUserAddressBo bo) {
        PUserAddress update = BeanUtil.toBean(bo, PUserAddress.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            if ("1".equals(update.getIsDefault())) {
                this.baseMapper.update(null, new LambdaUpdateWrapper<PUserAddress>().set(PUserAddress::getIsDefault, "0").eq(PUserAddress::getUid, LoginHelper.getUserId()));
                this.baseMapper.update(null, new LambdaUpdateWrapper<PUserAddress>().set(PUserAddress::getIsDefault, "1").eq(PUserAddress::getId, update.getId()));
            }
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PUserAddress entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除收货人地址
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public PUserAddress defaultAddress() {
        return this.baseMapper.selectOne(new LambdaQueryWrapper<PUserAddress>().eq(PUserAddress::getIsDefault, "1")
            .eq(PUserAddress::getUid, LoginHelper.getUserId())
            .eq(PUserAddress::getDelFlag, "0"));
    }

    @Override
    public List<PUserAddress> addressList() {
        return this.baseMapper.selectList(new LambdaQueryWrapper<PUserAddress>()
            .eq(PUserAddress::getUid, LoginHelper.getUserId())
            .eq(PUserAddress::getDelFlag, "0")
            .orderByDesc(PUserAddress::getIsDefault));
    }
}
